package com.zhida.jiyangcenter.dialog

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.fragment.app.DialogFragment
import chooongg.base.manager.app
import com.zhida.jiyangcenter.R

class AssessmentDialog : DialogFragment() {
    private var listener: ((Int) -> Unit?)? = null
    fun setListener(listener: ((Int) -> Unit?)) {
        this.listener = listener
    }

    @NonNull
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        val dialog = Dialog(requireContext(), R.style.BottomDialog)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) // 设置Content前设定
        dialog.setContentView(R.layout.dialog_assessment)
        dialog.setCanceledOnTouchOutside(false) // 外部点击取消

        // 设置宽度为屏宽, 靠近屏幕底部。
        val window = dialog.window
        window!!.setWindowAnimations(R.style.AnimBottom)
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val lp = window.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT // 宽度持平
        lp.gravity = Gravity.CENTER
        window.attributes = lp

        initView(dialog)
        return dialog
    }

    private fun initView(dialog: Dialog) {
        dialog.findViewById<TextView>(R.id.text_view_nv).setOnClickListener {
            listener?.invoke(0)
        }
        dialog.findViewById<TextView>(R.id.text_view_nan).setOnClickListener {
            listener?.invoke(1)
        }

        dialog.findViewById<ImageView>(R.id.iv_close).setOnClickListener {
            if (tag == "AssessmentDialog")
                app.activityTop.finish()
            dismiss()
        }
    }

}